<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="ares-p layui-form" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入姓名" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="male" title="男" checked>
            <input type="radio" name="sex" value="female" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出生日期</label>
        <div class="layui-input-block">
            <input name="birthday" id="birthday" placeholder="请选择出生日期" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input name="mobile" placeholder="请输入手机号" class="layui-input" lay-verify="required|phone">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">医院</label>
        <div class="layui-input-block">
            <input name="hospital" placeholder="请输入医院" class="layui-input" lay-verify="required" disabled>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">科室</label>
        <div class="layui-input-block">
            <select name="division" lay-verify="required">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">岗位</label>
        <div class="layui-input-block">
            <select name="job" lay-verify="required" lay-filter="job">
                <option value="therapist">实习老师</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分组</label>
        <div class="layui-input-block">
            <select name="treatment-group" lay-filter="treatment-group">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">导师</label>
        <div class="layui-input-block">
            <select name="tutor" lay-verify="required">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submit">提交表单</button>
            <span id="pwd-tip" class="layui-hide ares-text-red ares-text-xs">提示: 新建用户的默认密码为用户生日!</span>
        </div>
    </div>
</form>
</body>

<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['laydate', 'form', 'utils', 'mandyServices', 'store','qs'], function (laydate, form, utils, mandyServices, store, qs) {
        var queryParams = qs.parse(location.search.substr(1)),
            profile = store.get('profile'),
            currentHospital = profile.institution.hospital,
            currentDivision = profile.institution.division;

        laydate.render({
            elem: '#birthday',
            value: new Date()
        });

        form.on('submit(submit)', function (data) {
            var fields = data.field,
                service = null,
                $this = $(data.elem);
            console.log(fields, $this)
            if (queryParams.id) {
                service = modifyMedicalInfo
            } else {
                service = addMedicalInfo
            }
            if (!service) return;
            $this.prop('disabled', true).addClass('layui-btn-disabled')
            service(fields).then(function (res) {
                utils.msgSuccess(res.message)
                $this.prop('disabled', false).removeClass('layui-btn-disabled')
                utils.layer.closeAll('iframe');
                utils.ARES.event.emit('updateInternshipList');
            }).catch(function (err) {
                $this.prop('disabled', false).removeClass('layui-btn-disabled')
            })
            return false;
        });
        form.on('select(treatment-group)', function(data){
            var groupId = data.value;
            getTutor(groupId, null);
        });
        form.render();

        if (queryParams.id) {
            buildProfileForm(utils.ARES.cache.internship)
        } else {
            buildAddProfileForm();
            $('#pwd-tip').removeClass('layui-hide');
        }


        //修改成员信息
        function buildProfileForm(data) {
            var profileInfo = data.profile_info,
                hospital = data.hospital,
                division = data.division,
                treatmentGroup = data.treatment_group,
                teacherInfo = data.teacher_info;

            buildDivisionSelect(division.id, hospital.id);
            getTreatmentGroup(treatmentGroup.id, true);
            form.val("form", {
                name: profileInfo.name,
                sex: profileInfo.sex,
                birthday: profileInfo.birthday,
                mobile: data.mobile,
                hospital: hospital.name,
                division: division.id,
                job: data.job,
                'treatment-group': treatmentGroup ? treatmentGroup.id : '',
                tutor: teacherInfo.id,
            });
            form.render();

            getTutor(treatmentGroup ? treatmentGroup.id : null, teacherInfo.id)
        }

        //增减人员按钮单击事件
        function buildAddProfileForm() {
            form.val("form", {
                hospital: currentHospital.name
            });
            buildDivisionSelect(currentDivision.id, currentHospital.id);
            //获取治疗分组列表
            getTreatmentGroup(null);
        }

        //获取治疗分组列表
        function getTreatmentGroup(defaultVal,ignoreTutor) {
            var node = $('select[name="treatment-group"]');
            node.empty().append('<option value=""></option>');
            mandyServices.mandy_get_treatment_group().then(function (res) {
                var data = res.data;
                $.each(data.treat_groups, function (idx, val) {
                    var option = $("<option>");
                    option.text(val.name);
                    option.attr("value", val.id);
                    if (val.id == defaultVal) {
                        option.attr("selected", true);
                    }
                    node.append(option);
                });
                form.render('select');
                //获取实习医生导师列表
                if(!ignoreTutor)getTutor(form.val("form")['treatment-group']);
            })
        }

        //获取实习医生导师列表
        function getTutor(group_id, defaultVal) {
            var node = $('select[name="tutor"]');
            node.empty().append('<option value=""></option>');
            if(!group_id) return;
            mandyServices.mandy_get_treat_group_teacher(group_id).then(function (res) {
                var data = res.data;
                $.each(data.acc_list, function (idx, val) {
                    var option = $("<option>");
                    option.text(val.name);
                    option.attr("value", val.id);
                    if (val.id == defaultVal) {
                        option.attr("selected", true);
                    }
                    node.append(option);
                });
                form.render('select');
            })
        }

        //生成科室列表
        function buildDivisionSelect(defaultVal, hospitalId) {
            var node = $('select[name="division"]');
            node.empty().append('<option value=""></option>');
            mandyServices.mandy_get_division_list_syn(hospitalId).then(function (res) {
                var data = res.data;
                $.each(data.division_list, function (idx, val) {
                    var option = $("<option>");
                    option.text(val.name);
                    option.attr("value", val.id);
                    if (val.id == defaultVal) {
                        option.attr("selected", true);
                    }
                    node.append(option);
                });
                form.render('select');
            });
        }

        //修改成员信息
        function modifyMedicalInfo(data) {
            return mandyServices.mandy_put_medical_info(queryParams.id, data.name, data.sex, data.birthday, data.division, data.job, 3,
                data['treatment-group'])
        }

        //添加成员信息
        function addMedicalInfo(data) {
            return mandyServices.mandy_add_medical_info(data.mobile, data.birthday.replaceAll("-", ""), data.name, data.sex, data.birthday, data.division, data.job, 3, data.tutor,
                data['treatment-group']);
        }

    })
</script>


</html>